package com.stars.easyms.alarm.monitor;

import com.stars.easyms.alarm.client.EasyMsAlarmChannel;
import com.stars.easyms.alarm.client.EasyMsAlarmClientFactory;
import com.stars.easyms.alarm.constant.EasyMsAlarmConstant;
import com.stars.easyms.alarm.properties.EasyMsAlarmProperties;
import com.stars.easyms.base.bean.LazyLoadBean;
import com.stars.easyms.monitor.MonitorRequestHandler;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>className: EasyMsAlarmMonitorRequestHandler</p>
 * <p>description: EasyMs告警模块监控类</p>
 *
 * @author guoguifang
 * @version 1.7.0
 * @date 2020/11/26 11:13 上午
 */
public final class EasyMsAlarmMonitorRequestHandler implements MonitorRequestHandler {

    private final LazyLoadBean<EasyMsAlarmProperties> alarmPropertiesLazyLoadBean = new LazyLoadBean<>(EasyMsAlarmProperties.class);

    @Override
    public Map<String, Object> getMonitorInfo() {
        EasyMsAlarmProperties alarmProperties = alarmPropertiesLazyLoadBean.getBean();
        final Map<String, Object> map = new LinkedHashMap<>();
        // 先获取EasyMsAlarmProperties对象
        if (alarmProperties == null || !alarmProperties.isEnabled()) {
            map.put("enabled", false);
            return map;
        }
        map.put("enabled", true);
        map.put("defaultClientId", alarmProperties.getDefaultClientId());

        // 获取告警客户端
        List<EasyMsAlarmChannel> alarmChannelList = EasyMsAlarmClientFactory.getAlarmChannelList();
        if (!alarmChannelList.isEmpty()) {
            final Map<String, Map<String, Map<String, String>>> enabledAllAlarmChannelMap = new LinkedHashMap<>();
            alarmChannelList.forEach(channel -> {
                Map<String, Map<String, String>> alarmChannelMap = new LinkedHashMap<>(8);
                channel.getChannelClientInfo().forEach((id, client) -> alarmChannelMap.put(id, client.getClientInfo()));
                enabledAllAlarmChannelMap.put(channel.getChannelType(), alarmChannelMap);
            });
            map.put("channels", enabledAllAlarmChannelMap);
        }

        String plumeLogUrl = alarmProperties.getPlumeLog().getUrl();
        if (StringUtils.isNotBlank(plumeLogUrl)) {
            Map<String, String> plumeLogMap = new LinkedHashMap<>(4);
            plumeLogMap.put("url", plumeLogUrl);
            map.put("plumeLog", plumeLogMap);
            return map;
        }

        EasyMsAlarmProperties.Elk elk = alarmProperties.getElk();
        String kibanaUrl = elk.getKibanaUrl();
        if (StringUtils.isNotBlank(kibanaUrl)) {
            Map<String, Object> elkMap = new LinkedHashMap<>(8);
            elkMap.put("kibanaUrl", kibanaUrl);
            elkMap.put("searchColumns", elk.getColumns());
            elkMap.put("defaultGlobalIndex", elk.getGlobalIndex());
            elkMap.put("defaultIndex", elk.getIndex());
            elkMap.put("indexs", elk.getIndexs());
            map.put("elk", elkMap);
        }
        return map;
    }

    @Override
    public String getModuleName() {
        return EasyMsAlarmConstant.MODULE_NAME;
    }

    @Override
    public int getOrder() {
        return 1;
    }

}